*******************************
*         Creating the        *
* Subnational Democracy Scale *
*           (SEDS)            *
*******************************

****Set your working directory and load data****
cd "C:\Users\aif08\Google Drive (afidalgo@ncf.edu)\Research\Measurement paper\perceptions of democracy\Governance\R& R comments\new analysis\Replication Files"
*Change the code above to match your working directory

use "Subnational Electoral Democracy Scale.dta", clear


*This dataset includes the SEDS and alternative SEDS
*If you would like to run this code complete run the following commands:
drop SEDS SEDS2





***Generating National variables for standardization
	*Basic steps of this code
		*1. Generate an empty spline variable that will contain the predicted national values
		*2. For each country in the dataset, estimate a regression where the national electoral variable is the dependent variable and year is the independnet variable
		*3. Predice the national electoral variable based on that regression
		*4. fill the empty spline variable with that predicted value for each country
		*5. drop the prediction so I can rerun this analysis for each country
		*6. replicate steps 1-5 for all national electoral variable



set more off

*ollhi- Inverse National Legislative Herfindahl-Hirschman Index
gen splineINllhi=.

reg INllhi yearcount2 if cntry==1
predict yhat
replace splineINllhi=yhat if cntry==1
drop yhat

reg INllhi yearcount2 if cntry==2
predict yhat
replace splineINllhi=yhat if cntry==2
drop yhat

reg INllhi yearcount2 if cntry==3
predict yhat
replace splineINllhi=yhat if cntry==3
drop yhat

reg INllhi yearcount2 if cntry==4
predict yhat
replace splineINllhi=yhat if cntry==4
drop yhat

reg INllhi yearcount2 if cntry==5
predict yhat
replace splineINllhi=yhat if cntry==5
drop yhat

reg INllhi yearcount2 if cntry==6
predict yhat
replace splineINllhi=yhat if cntry==6
drop yhat

reg INllhi yearcount2 if cntry==7
predict yhat
replace splineINllhi=yhat if cntry==7
drop yhat

reg INllhi yearcount2 if cntry==8
predict yhat
replace splineINllhi=yhat if cntry==8
drop yhat

reg INllhi yearcount2 if cntry==9
predict yhat
replace splineINllhi=yhat if cntry==9
drop yhat

reg INllhi yearcount2 if cntry==10
predict yhat
replace splineINllhi=yhat if cntry==10
drop yhat

reg INllhi yearcount2 if cntry==11
predict yhat
replace splineINllhi=yhat if cntry==11
drop yhat

reg INllhi yearcount2 if cntry==12
predict yhat
replace splineINllhi=yhat if cntry==12
drop yhat

replace splineINllhi=. if INllhi==.

gen splollhi=ollhi-splineINllhi
 

 
 
 

 *oghhi- Inverse National Exectuvie Herfindahl-Hirschman Index
gen splineINhhi =.

reg INhhi  yearcount2 if cntry==1
predict yhat
replace splineINhhi =yhat if cntry==1
drop yhat

reg INhhi  yearcount2 if cntry==2
predict yhat
replace splineINhhi =yhat if cntry==2
drop yhat

reg INhhi  yearcount2 if cntry==3
predict yhat
replace splineINhhi =yhat if cntry==3
drop yhat

reg INhhi  yearcount2 if cntry==4
predict yhat
replace splineINhhi =yhat if cntry==4
drop yhat

reg INhhi  yearcount2 if cntry==5
predict yhat
replace splineINhhi =yhat if cntry==5
drop yhat

reg INhhi  yearcount2 if cntry==6
predict yhat
replace splineINhhi =yhat if cntry==6
drop yhat

reg INhhi  yearcount2 if cntry==7
predict yhat
replace splineINhhi =yhat if cntry==7
drop yhat

reg INhhi  yearcount2 if cntry==8
predict yhat
replace splineINhhi =yhat if cntry==8
drop yhat

reg INhhi  yearcount2 if cntry==9
predict yhat
replace splineINhhi =yhat if cntry==9
drop yhat

reg INhhi  yearcount2 if cntry==10
predict yhat
replace splineINhhi =yhat if cntry==10
drop yhat

reg INhhi  yearcount2 if cntry==11
predict yhat
replace splineINhhi =yhat if cntry==11
drop yhat

reg INhhi  yearcount2 if cntry==12
predict yhat
replace splineINhhi =yhat if cntry==12
drop yhat

replace splineINhhi =. if INhhi ==.

gen sploghhi=oghhi -splineINhhi 




*Nexecoppvote- National Opposition party vote share
gen splineNexecoppvote =.

reg Nexecoppvote  yearcount2 if cntry==1
predict yhat
replace splineNexecoppvote =yhat if cntry==1
drop yhat

reg Nexecoppvote  yearcount2 if cntry==2
predict yhat
replace splineNexecoppvote =yhat if cntry==2
drop yhat

reg Nexecoppvote  yearcount2 if cntry==3
predict yhat
replace splineNexecoppvote =yhat if cntry==3
drop yhat

reg Nexecoppvote  yearcount2 if cntry==4
predict yhat
replace splineNexecoppvote =yhat if cntry==4
drop yhat

reg Nexecoppvote  yearcount2 if cntry==5
predict yhat
replace splineNexecoppvote =yhat if cntry==5
drop yhat

reg Nexecoppvote  yearcount2 if cntry==6
predict yhat
replace splineNexecoppvote =yhat if cntry==6
drop yhat

reg Nexecoppvote  yearcount2 if cntry==7
predict yhat
replace splineNexecoppvote =yhat if cntry==7
drop yhat

reg Nexecoppvote  yearcount2 if cntry==8
predict yhat
replace splineNexecoppvote =yhat if cntry==8
drop yhat

reg Nexecoppvote  yearcount2 if cntry==9
predict yhat
replace splineNexecoppvote =yhat if cntry==9
drop yhat

reg Nexecoppvote  yearcount2 if cntry==10
predict yhat
replace splineNexecoppvote =yhat if cntry==10
drop yhat

reg Nexecoppvote  yearcount2 if cntry==11
predict yhat
replace splineNexecoppvote =yhat if cntry==11
drop yhat

reg Nexecoppvote  yearcount2 if cntry==12
predict yhat
replace splineNexecoppvote =yhat if cntry==12
drop yhat

replace splineNexecoppvote =. if Nexecoppvote ==.

gen sploppvote=oppvote -splineNexecoppvote 


*Npoppseats-  National opposition party seat share in the legislature
gen splineNpoppseats =.

reg Npoppseats  yearcount2 if cntry==1
predict yhat
replace splineNpoppseats =yhat if cntry==1
drop yhat

reg Npoppseats  yearcount2 if cntry==2
predict yhat
replace splineNpoppseats =yhat if cntry==2
drop yhat

reg Npoppseats  yearcount2 if cntry==3
predict yhat
replace splineNpoppseats =yhat if cntry==3
drop yhat

reg Npoppseats  yearcount2 if cntry==4
predict yhat
replace splineNpoppseats =yhat if cntry==4
drop yhat

reg Npoppseats  yearcount2 if cntry==5
predict yhat
replace splineNpoppseats =yhat if cntry==5
drop yhat

reg Npoppseats  yearcount2 if cntry==6
predict yhat
replace splineNpoppseats =yhat if cntry==6
drop yhat

reg Npoppseats  yearcount2 if cntry==7
predict yhat
replace splineNpoppseats =yhat if cntry==7
drop yhat

reg Npoppseats  yearcount2 if cntry==8
predict yhat
replace splineNpoppseats =yhat if cntry==8
drop yhat

reg Npoppseats  yearcount2 if cntry==9
predict yhat
replace splineNpoppseats =yhat if cntry==9
drop yhat

reg Npoppseats  yearcount2 if cntry==10
predict yhat
replace splineNpoppseats =yhat if cntry==10
drop yhat

reg Npoppseats  yearcount2 if cntry==11
predict yhat
replace splineNpoppseats =yhat if cntry==11
drop yhat

reg Npoppseats  yearcount2 if cntry==12
predict yhat
replace splineNpoppseats =yhat if cntry==12
drop yhat

replace splineNpoppseats =. if Npoppseats ==.

gen splpoppseats=poppseats -splineNpoppseats 


*Npartyturnrate- National party turnover rate
gen splineNpartyturnrate =.

reg Npartyturnrate  yearcount2 if cntry==1
predict yhat
replace splineNpartyturnrate =yhat if cntry==1
drop yhat

reg Npartyturnrate  yearcount2 if cntry==2
predict yhat
replace splineNpartyturnrate =yhat if cntry==2
drop yhat

reg Npartyturnrate  yearcount2 if cntry==3
predict yhat
replace splineNpartyturnrate =yhat if cntry==3
drop yhat

reg Npartyturnrate  yearcount2 if cntry==4
predict yhat
replace splineNpartyturnrate =yhat if cntry==4
drop yhat

reg Npartyturnrate  yearcount2 if cntry==5
predict yhat
replace splineNpartyturnrate =yhat if cntry==5
drop yhat

reg Npartyturnrate  yearcount2 if cntry==6
predict yhat
replace splineNpartyturnrate =yhat if cntry==6
drop yhat

reg Npartyturnrate  yearcount2 if cntry==7
predict yhat
replace splineNpartyturnrate =yhat if cntry==7
drop yhat

reg Npartyturnrate  yearcount2 if cntry==8
predict yhat
replace splineNpartyturnrate =yhat if cntry==8
drop yhat

reg Npartyturnrate  yearcount2 if cntry==9
predict yhat
replace splineNpartyturnrate =yhat if cntry==9
drop yhat

reg Npartyturnrate  yearcount2 if cntry==10
predict yhat
replace splineNpartyturnrate =yhat if cntry==10
drop yhat

reg Npartyturnrate  yearcount2 if cntry==11
predict yhat
replace splineNpartyturnrate =yhat if cntry==11
drop yhat

reg Npartyturnrate  yearcount2 if cntry==12
predict yhat
replace splineNpartyturnrate =yhat if cntry==12
drop yhat

replace splineNpartyturnrate =. if Npartyturnrate ==.


gen splpartyturnrate=partyturnrate -splineNpartyturnrate 
 
 

*NHexec-  National Shannon's H measuring the number of parties competing in executive elections
 
gen splineNHexec =.

reg NHexec  yearcount2 if cntry==1
predict yhat
replace splineNHexec =yhat if cntry==1
drop yhat

reg NHexec  yearcount2 if cntry==2
predict yhat
replace splineNHexec =yhat if cntry==2
drop yhat

reg NHexec  yearcount2 if cntry==3
predict yhat
replace splineNHexec =yhat if cntry==3
drop yhat

reg NHexec  yearcount2 if cntry==4
predict yhat
replace splineNHexec =yhat if cntry==4
drop yhat

reg NHexec  yearcount2 if cntry==5
predict yhat
replace splineNHexec =yhat if cntry==5
drop yhat

reg NHexec  yearcount2 if cntry==6
predict yhat
replace splineNHexec =yhat if cntry==6
drop yhat

reg NHexec  yearcount2 if cntry==7
predict yhat
replace splineNHexec =yhat if cntry==7
drop yhat

reg NHexec  yearcount2 if cntry==8
predict yhat
replace splineNHexec =yhat if cntry==8
drop yhat

reg NHexec  yearcount2 if cntry==9
predict yhat
replace splineNHexec =yhat if cntry==9
drop yhat

reg NHexec  yearcount2 if cntry==10
predict yhat
replace splineNHexec =yhat if cntry==10
drop yhat

reg NHexec  yearcount2 if cntry==11
predict yhat
replace splineNHexec =yhat if cntry==11
drop yhat

reg NHexec  yearcount2 if cntry==12
predict yhat
replace splineNHexec =yhat if cntry==12
drop yhat

replace splineNHexec =. if NHexec ==.

gen splHgov=Hgov -splineNHexec 


*Hleg-  National Shannon's H measuring the number of parties represented in the legislature 
gen splineNHleg =.

reg NHleg  yearcount2 if cntry==1
predict yhat
replace splineNHleg =yhat if cntry==1
drop yhat

reg NHleg  yearcount2 if cntry==2
predict yhat
replace splineNHleg =yhat if cntry==2
drop yhat

reg NHleg  yearcount2 if cntry==3
predict yhat
replace splineNHleg =yhat if cntry==3
drop yhat

reg NHleg  yearcount2 if cntry==4
predict yhat
replace splineNHleg =yhat if cntry==4
drop yhat

reg NHleg  yearcount2 if cntry==5
predict yhat
replace splineNHleg =yhat if cntry==5
drop yhat

reg NHleg  yearcount2 if cntry==6
predict yhat
replace splineNHleg =yhat if cntry==6
drop yhat

reg NHleg  yearcount2 if cntry==7
predict yhat
replace splineNHleg =yhat if cntry==7
drop yhat

reg NHleg  yearcount2 if cntry==8
predict yhat
replace splineNHleg =yhat if cntry==8
drop yhat

reg NHleg  yearcount2 if cntry==9
predict yhat
replace splineNHleg =yhat if cntry==9
drop yhat

reg NHleg  yearcount2 if cntry==10
predict yhat
replace splineNHleg =yhat if cntry==10
drop yhat

reg NHleg  yearcount2 if cntry==11
predict yhat
replace splineNHleg =yhat if cntry==11
drop yhat

reg NHleg  yearcount2 if cntry==12
predict yhat
replace splineNHleg =yhat if cntry==12
drop yhat

replace splineNHleg =. if NHleg ==.

gen splHleg=Hleg -splineNHleg 



 
****Generating the latent variables
*This variable is already in the dataset, please drop SEDS and SEDS2 if you would like to run this code


*For each structural equation model (SEM) the code does the following
	*1. First line estimates the SEM
	*2. The second line estimates goodness of fit statistics for the SEM
	*3. The third line predicts the SEDS using the SEM
	*4. The fourth line sets observations to missing if they are missing any of the components of the SEDS
	*5. Fifth line labels the variable


sem (DEM->sploghhi splollhi sploppvote splpoppseats splpartyturnrate), cov (e.splollhi*e.splpoppseats) cov(e.sploghhi*e.sploppvote) cov(e.sploghhi*e.splollhi)  variance(DEM@1) mean(DEM@0)
estat gof, stats(all)
predict SEDS, laten(DEM)
replace SEDS=. if sploghhi==. | splollhi==. |sploppvote==. | splpoppseat==. | splpartyturnrate==.
label variable SEDS "Subnational Electoral Democracy Scale"


sem (DEM->splHgov splHleg sploppvote splpoppseats splpartyturnrate), cov (e.splHleg*e.splpoppseats) cov(e.splHgov*e.sploppvote)  cov(e.splHleg*e.splHgov)  variance(DEM@1) mean(DEM@0)
estat gof, stats(all)
predict SEDS2, laten(DEM)
replace SEDS2=. if splHleg==. | splHgov==. |sploppvote==. | splpoppseat==. | splpartyturnrate==.
label variable SEDS2 "Subnational Electoral Democracy Scale with Hscores"


*Estimating the average SEDS and alternative SEDS for each state in each country
egen aveSEDS=mean(SEDS), by(country state) 
egen aveSEDS2=mean(SEDS2), by(country state) 

*Generating a boxplot displaying the distribution of the SEDS across countries
graph hbox SEDS, over(cntry, total) ///
            graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
			title("Distribution of the SEDS" "Across Countries") ///
			ytitle("Subnational Electoral Democracy Scale") 
			
graph export "demsummary.eps", as(eps) preview(on) replace


*Generating the plots looking at the average SEDS schore in Oaxaca Mexico and Catamarca Argentina over time 
twoway connect SEDS year if state=="Oaxaca" & year>=2000, ///
            graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
			title("Subnational Democracy Scale"  "Oaxaca, Mexico") ///
			ytitle("Subnational Democracy Scale") ///
			xline(2010, lpattern(dash_dot)) ///
			xtitle("year") 
	
graph export "oaxaca.eps", as(eps) preview(on) replace
			
			
twoway connect SEDS year if state=="Catamarca", ///
            graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
			title("Subnational Democracy Scale"  "Catamarca, Argentina") ///
			ytitle("Subnational Democracy Scale") ///
			xline(1991, lpattern(dash_dot)) ///
			xtitle("year") 
graph export "catamarca.eps", as(eps) preview(on) replace






